package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.Category;
import com.itheima.entity.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import io.micrometer.common.util.StringUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 套餐 前端控制器
 * </p>
 *
 * @author fengzhu
 * @since 2024-10-10
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;
    /**
     * 实现套餐查询,并展示套餐分类
     * @param page 页数
     * @param pageSize 一页上的大小
     * @param name 一页上的名字
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //初始化Page对象
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();


        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotEmpty(name)) {
            wrapper.like(Setmeal::getName, name);
        }
        setmealService.page(pageInfo, wrapper);
        //基础的属性page,pagesize
        BeanUtils.copyProperties(pageInfo, setmealDtoPage,"records");

        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = records.stream().map((item)->{
            //把一个SetmealDTO对象填好
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Category category = categoryService.getById(setmealDto.getCategoryId());
            if (category != null) {
                setmealDto.setCategoryName(category.getName());
            }
           return setmealDto;
        }).toList();
        setmealDtoPage.setRecords(list);
        return R.success(setmealDtoPage);
    }

    /**
     * 实现批量停售，批量启售操作
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable("status") int status,@RequestParam List<Long> ids){

        setmealService.updateStatusById(status,ids);
        return R.success("操作成功") ;
    }

    /**
     *实现单个删除和批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        if(ids ==null || ids.isEmpty()){
            log.warn("未选择任何想要删除的项目");
            R.error("未选择任何想要删除的项目");
        }

        boolean isRemoved = setmealService.removeByIds(ids);
        if(isRemoved){
            log.info("成功删除了{}条记录",ids.size());
            return R.success("删除成功");
        }else {
            log.error("删除失败，ID 列表：{}", ids);
            return R.error("删除失败，可能部分项目未找到");
        }

    }

    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody SetmealDto setmealDto){
        Long empId =(Long) request.getSession().getAttribute("employee");
        setmealDto.setCreateUser(empId);
        setmealDto.setUpdateUser(empId);
        setmealService.save(setmealDto);
        return R.success("成功");
    }

    /**
     * 实现数据的回写
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getByIdWithDish(@PathVariable Long id){
       SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        //从dish
        return R.success(setmealDto);
    }
    @PutMapping
    public R<String> updateWithSetmealDish(HttpServletRequest request,@RequestBody SetmealDto setmealDto){
        //再去转化成setmeal
        Long empid =(Long) request.getSession().getAttribute("employee");
       setmealService.updateWithSetmealDish(setmealDto,empid);
       return R.success("成功");
    }
}
